package com.ty.tour.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.tour.common.utils.PagedResult;
import com.ty.tour.common.utils.ReturnData;
import com.ty.tour.pojo.Product;
import com.ty.tour.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @Description: 商品Controller
 * @Author: xiexiaoping
 * @Date: 2020/3/21 11:28
 **/
@Api(value = "商品Controller", tags = "商品查看")
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /***
     * @MethodName: list
     * @Description: 商品列表
     * @Param: []
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/21 11:30
     **/
    @PostMapping("list")
    @ApiOperation(value = "查看所有商品", notes = "商品实体")
    public ReturnData list() {
        final ReturnData returnData = new ReturnData();
        final Map<String, Object> params = new HashMap<>();
        try {
            List<Product> products = productService.list();
            return returnData.success(PagedResult.setPageResult(products,10), 1);
        } catch (Exception e) {
            e.printStackTrace();
            return returnData.fail();
        }
    }

    /***
     * @MethodName: getProductInfo
     * @Description:
     * @Param: [id]
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/21 11:48
     **/
    @PostMapping("get-product-info")
    @ApiOperation(value = "查看商品详情", notes = "商品类实体")
    @ApiImplicitParam(value = "商品编号", name = "id", dataType = "Integer", paramType = "query", required = true)
    public ReturnData getProductInfo(Integer id) {
        final ReturnData returnData = new ReturnData();
        try {
            if (id == null) {
                return returnData.fail();
            }
            Product product = productService.getById(id);
            if (product == null) {
                return returnData.fail();
            }
            return returnData.success(product, 1);
        } catch (Exception e) {
            e.printStackTrace();
            return returnData.fail();
        }
    }
}
